package com.example.stock.entity.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.stock.comm.Cons;
import com.example.stock.comm.FreqEnum;
import com.example.stock.entity.eo.ProBarFundEo;
import com.example.stock.entity.mapper.ProBarFundMapper;
import com.example.stock.entity.service.IProBarFundService;
import com.example.stock.vo.DataVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProBarFundServiceImpl extends ServiceImpl<ProBarFundMapper, ProBarFundEo> implements IProBarFundService {

    @Resource
    private ProBarFundMapper proBarFundMapper;

    public List<ProBarFundEo> getAll(String tsCode){
        return proBarFundMapper.selectList(Wrappers.<ProBarFundEo>lambdaQuery()
                .eq(ProBarFundEo::getTsCode, tsCode)
                .orderByAsc(ProBarFundEo::getTradeTime));

    }

    //获取30分钟收盘价
    public List<DataVo> getRowData30(String tsCode, LocalDateTime startTime){
        final LocalDateTime endTime = startTime == null ? LocalDateTime.MIN : startTime;
        List<ProBarFundEo> proBarFundEos = getAll(tsCode);
        return proBarFundEos.stream()
                .filter(t -> FreqEnum.MIN30.listTime().contains(t.getTradeTime().format(Cons.formatHHmm)))
                .filter(t -> t.getTradeTime().compareTo(endTime) >= 0)
                .map(t -> {
                    DataVo dataVo = new DataVo();
                    dataVo.setTradeTime(t.getTradeTime());
                    dataVo.setPrice(t.getClose());
                    return dataVo;
                }).collect(Collectors.toList());
    }


    public List<DataVo> getRowData60(String tsCode, LocalDateTime startTime) {
        final LocalDateTime endTime = startTime == null ? LocalDateTime.MIN : startTime;
        List<ProBarFundEo> proBarFundEos = getAll(tsCode);
        return proBarFundEos.stream()
                .filter(t -> Cons.time60Min.contains(t.getTradeTime().format(Cons.formatHHmm)))
                .filter(t -> t.getTradeTime().compareTo(endTime) >= 0)
                .map(t -> {
                    DataVo dataVo = new DataVo();
                    dataVo.setTradeTime(t.getTradeTime());
                    dataVo.setPrice(t.getClose());
                    return dataVo;
                }).collect(Collectors.toList());
    }
}
